<?php

// This hook sets the locale.language and locale.lang config values
// based on the language found in the first segment of the URL.

Event::add('system.routing', 'site_lang');

function site_lang()
{
	global $lang;
	
    // Array of allowed languages
    $locales = Config::item('locale.allowed_locales');
	
	if(cookie::get('lang'))
		$lang = cookie::get('lang');
	else
		$lang = 'cn';

	// If the URL does not contain a language in its first segment...
	//if ( ! preg_match('#^[a-z]{2}(?:/|$)#i', url::current()))
	//{
	//	if((String)cookie::get('lang') == '')
	//		$lang = 'cn';
	//	else
	//		$lang = (String)cookie::get('lang');
	//}
	//else
	//{
	    // Extract language from URL
	//    $lang = strtolower(substr(url::current(), 0, 2));
	//}

    // Invalid language is given in the URL
    if ( ! array_key_exists($lang, $locales))
    {
        // Look for default alternatives and store them in order
        // of importance in the $new_langs array:
        //  1. cookie
        //  2. http_accept_language header
        //  3. default lang

        // Look for cookie
        $new_langs[] = (string) cookie::get('lang');

        // Look for HTTP_ACCEPT_LANGUAGE
        if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
        {
            foreach(explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $part)
            {
                $new_langs[] = substr($part, 0, 2);
            }
        }

        // Lowest priority goes to default language
        $new_langs[] = 'cn';

        // Now loop through the new languages and pick out the first valid one
        foreach(array_unique($new_langs) as $new_lang)
        {
            $new_lang = strtolower($new_lang);

            if (array_key_exists($new_lang, $locales))
            {
                $lang = $new_lang;
                break;
            }
        }

        // Redirect to URL with valid language
        url::redirect($lang.substr(url::current(), 2));
    }

    // Store locale config values
    Config::set('locale.lang', $lang);
    Config::set('locale.language', $locales[$lang]);

    // Overwrite setlocale which has already been set before in Kohana::setup()
    setlocale(LC_ALL, Config::item('locale.language').'.UTF-8');

    // Finally set a language cookie for 6 months
    cookie::set('lang', $lang, 15768000);
}
 